var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import React, { useEffect, useState } from "react";
import { PickerView, PickerViewColumn, View } from "@tarojs/components";
/**
 * Picker
 */
function Picker(_a) {
    var dataSource = _a.dataSource, defaultValue = _a.defaultValue, _b = _a.column, column = _b === void 0 ? 1 : _b, linkage = _a.linkage, onChange = _a.onChange, columnStyle = _a.columnStyle, activeTextStyle = _a.activeTextStyle, textStyle = _a.textStyle, style = _a.style, indicatorStyle = _a.indicatorStyle, indicatorClass = _a.indicatorClass, maskClass = _a.maskClass, maskStyle = _a.maskStyle;
    // 设置picker默认植
    var _c = __read(useState([]), 2), pickerValue = _c[0], setPickerValue = _c[1];
    // 1 2 3列数据
    var _d = __read(useState([]), 2), pickerData = _d[0], setPickerData = _d[1];
    useEffect(function () {
        if (dataSource && Array.isArray(dataSource) && dataSource.length > 0) {
            var pickerData_1 = []; // picker数据源
            var pickerValue_1 = Array(column).fill(0); // picker选中默认值
            // 1. 设置 pickerData
            if (linkage) {
                // 联动
                var ds = []; // 取一维数组
                if (Array.isArray(dataSource[0])) {
                    // 多维数组
                    ds = dataSource[0];
                }
                else {
                    ds = dataSource;
                }
                var dsT = ds;
                if (defaultValue && defaultValue.length > 0) {
                    var _loop_1 = function (index) {
                        var element = defaultValue[index];
                        if (dsT && dsT.length > 0 && element) {
                            var index_1 = dsT.findIndex(function (item) { return item.value === element; });
                            var value = index_1 >= 0 ? index_1 : 0;
                            // pickerValue.push(value)
                            pickerData_1.push(dsT);
                            dsT = dsT[value].children;
                        }
                        else {
                            console.warn("zdx pickerData defaultValue column 数据长度不一致");
                            return "break";
                        }
                    };
                    // 有默认值，查找colum个数组
                    for (var index = 0; index < column; index++) {
                        var state_1 = _loop_1(index);
                        if (state_1 === "break")
                            break;
                    }
                }
                else {
                    // 没有默认值直接装填
                    for (var index = 0; index < column; index++) {
                        if (dsT && dsT.length > 0) {
                            pickerData_1.push(dsT);
                            dsT = dsT[0].children;
                        }
                    }
                }
                setPickerData(pickerData_1);
            }
            else {
                // 非联动
                if (Array.isArray(dataSource[0])) {
                    // 多维数组
                    pickerData_1 = dataSource.slice(0, column);
                }
                else {
                    // 一维数组
                    pickerData_1 = [dataSource];
                }
                setPickerData(pickerData_1);
            }
            // 2. 设置默认值
            if (defaultValue && defaultValue.length > 0) {
                pickerValue_1 = []; // 置空后重新装填
                var _loop_2 = function (index) {
                    var element = defaultValue[index];
                    var data = pickerData_1[index];
                    if (data && element !== undefined) {
                        var index_2 = data.findIndex(function (item) { return item.value === element; });
                        pickerValue_1.push(index_2 >= 0 ? index_2 : 0);
                    }
                    else {
                        return "break";
                    }
                };
                for (var index = 0; index < column; index++) {
                    var state_2 = _loop_2(index);
                    if (state_2 === "break")
                        break;
                }
            }
            setPickerValue(pickerValue_1);
            // 主动回调onChange
            var values = getChangeValues(pickerData_1, pickerValue_1, column);
            onChange && onChange(values);
        }
    }, [dataSource, column, linkage, defaultValue, onChange]);
    var onPickerChange = function (e) {
        var indexs = e.detail.value;
        var pickerDataT = pickerData;
        if (linkage) {
            var pickerDataTT = [];
            var needReset = false;
            var dsT = pickerData[0]; // 第1列不会变
            for (var index = 0; index < column; index++) {
                var id = indexs[index]; // 当前更新后的值 index
                var pv = pickerValue[index]; // 上一次的值 index
                // 如果linkage=true，并且当前的变动的是column -1的列就需要 setPickerData
                if (id != pv && index < column - 1) {
                    if (!needReset)
                        needReset = true;
                    // 同时需要将下个级联列的index置0
                    if (indexs.length >= index + 1) {
                        indexs[index + 1] = 0;
                    }
                }
                dsT && pickerDataTT.push(dsT);
                dsT = dsT && dsT[id].children;
            }
            if (needReset) {
                pickerDataT = pickerDataTT;
                setPickerData(pickerDataTT);
            }
        }
        // 拿到最终的pickerDataT
        setPickerValue(indexs);
        var values = getChangeValues(pickerDataT, indexs, column);
        onChange && onChange(values);
    };
    /**
     * 获取onChange数据
     * @param datas 原始数据
     * @param indexs 每列选中索引
     * @param column 列数
     */
    var getChangeValues = function (datas, indexs, column) {
        var values = [];
        for (var index = 0; index < column; index++) {
            var elements = datas[index]; // 列数组
            var row = indexs[index]; // 列对应的行索引
            var value = void 0;
            if (elements && elements[row]) {
                value = elements[row];
                values.push(value);
            }
        }
        return values;
    };
    var getStyle = function (index, col) {
        var active = index === pickerValue[col];
        if (active && activeTextStyle) {
            return activeTextStyle;
        }
        return __assign({ overflow: "hidden", whiteSpace: "nowrap", textOverflow: "ellipsis", height: 34 + "px", lineHeight: 34 + "px", fontWeight: active ? 600 : 400, color: active ? "#37D9CE" : "#000000" }, textStyle);
    };
    return (React.createElement(PickerView, { style: __assign({ height: 430 + "rpx", backgroundColor: "#ffffff" }, style), indicatorStyle: indicatorStyle, indicatorClass: indicatorClass, maskClass: maskClass, maskStyle: maskStyle, onChange: onPickerChange, value: pickerValue }, pickerData && pickerData.length > 0
        ? pickerData.map(function (d, index) {
            return (React.createElement(PickerViewColumn, { key: index, style: __assign({ textAlign: "center" }, columnStyle) }, d.map(function (item, i) {
                return (React.createElement(View, { style: getStyle(i, index), key: i }, item.label));
            })));
        })
        : null));
}
export default Picker;
